package com.atguigu.yygh.user.controller.user;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.utils.JwtHelper;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 就诊人表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-03-27
 */
@RestController
@RequestMapping("/user/patient")
public class PatientController {
    @Autowired
    private PatientService patientService;

    //增
    @PostMapping("/save")
    public R save(@RequestBody Patient patient, @RequestHeader String token){
        Long userId = JwtHelper.getUserId(token);
        patient.setUserId(userId);
        patientService.save(patient);
        return R.ok();
    }

    //删
    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable Long id){
        patientService.removeById(id);
        return R.ok();
    }


    //改回显(查询)
    @GetMapping("/edit/{id}")
    public R edit(@PathVariable Long id){
        Patient patient = patientService.edit(id); //获取就诊人信息
        return R.ok().data("item",patient);
    }


    //修改
    @PutMapping("/update")
    public R update(@RequestBody Patient patient){
        patientService.updateById(patient);
        return R.ok();
    }

    //查当前用户底下的所有就诊人信息
    @GetMapping("/all")
    public R findAll(@RequestHeader String token){
        Long userId = JwtHelper.getUserId(token);
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<Patient>();
        queryWrapper.eq("user_id", userId);
        List<Patient> list = patientService.getPatientlist(queryWrapper);
        return R.ok().data("items",list);
    }

    @GetMapping("/patient/{id}")
    public Patient patient(@PathVariable Long id){
       return patientService.edit(id); //获取就诊人信息
    }
}

